﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using H2.NET.Common;
using H2.NET.Data.Common;
using H2.NET.Web.Pages.SMD.Model;

namespace H2.NET.Web.Pages.SMD.Service
{
    public static class AuthIDService
    {
        public static IList<t_smd_AuthID> GetList(string province, string storeType)
        {
            var authIds = (t_smd_AuthID._Province == province & t_smd_AuthID._StoreType == storeType).List<t_smd_AuthID>();

            return authIds;
        }

        public static string GetNextNumber(string province, string storeType, bool preview)
        {
            var authId = (t_smd_AuthID._Province == province & t_smd_AuthID._StoreType == storeType).ListFirst<t_smd_AuthID>(Storage.Context3,
                " Sequence desc ");

            var newAuthId = new t_smd_AuthID
            {
                Sequence = authId == null && storeType == "EPS" ? 0001 : (authId == null && storeType != "EPS" ? 1001 : authId.Sequence + 1),//chris wang 10.20
                Province = province,
                StoreType = storeType
            };

            //if ((newAuthId.Sequence / 1000) == 4) //跳过千位4
            //{
            //    newAuthId.Sequence = newAuthId.Sequence + 1000;
            //}

            //if ((newAuthId.Sequence % 1000 / 100) == 4) //跳过百位4
            //{
            //    newAuthId.Sequence = newAuthId.Sequence + 100;
            //}

            //if ((newAuthId.Sequence % 100 / 10) == 4) //跳过十位4
            //{
            //    newAuthId.Sequence = newAuthId.Sequence + 10;
            //}

            if ((newAuthId.Sequence % 10) == 4) //跳过个4
            {
                newAuthId.Sequence = newAuthId.Sequence + 1;
            }

            string provinceCode;
            if (!ProvinceMap().TryGetValue(province, out provinceCode))
            {
                return string.Empty;
            }

            string storeTypeCode;
            if (!StoreTypeMap().TryGetValue(storeType, out storeTypeCode))
            {
                return string.Empty;
            }

            newAuthId.Number = "A" + provinceCode + storeTypeCode + newAuthId.Sequence.ToString().PadLeft(4,'0');

            if (!preview) //如果不是预览，就插入数据库
            {
                Storage.Save(Storage.Context3, newAuthId);
            }

            return newAuthId.Number;
        }
        public static string GetNextNumber(string province, string storeType, bool preview, List<string> excludeAuthId)
        {
            var identity = 1;
            var newAuthId = new t_smd_AuthID();
            var isloop = true;
            // 排除同一页面上生成相同的序列号
            while (isloop)
            {
                newAuthId = GenerateAuthId(province, storeType, identity++);
                if (excludeAuthId != null && !excludeAuthId.Contains(newAuthId.Number))
                {
                    isloop = false;
                }
            }
            if (!preview) //如果不是预览，就插入数据库
            {
                Storage.Save(Storage.Context3, newAuthId);
            }

            excludeAuthId.Add(newAuthId.Number);

            return newAuthId.Number;
        }

        public static t_smd_AuthID GenerateAuthId(string province, string storeType, int identity)
        {
            var authId = (t_smd_AuthID._Province == province & t_smd_AuthID._StoreType == storeType).ListFirst<t_smd_AuthID>(Storage.Context3,
                " Sequence desc ");

            var newAuthId = new t_smd_AuthID
            {
                //Sequence = authId == null ? 1001 : authId.Sequence + identity,
                Sequence = authId == null && storeType == "EPS" ? 0001 : (authId == null && storeType != "EPS" ? 1001 : authId.Sequence + identity),//chris wang 10.20
                Province = province,
                StoreType = storeType
            };

            if ((newAuthId.Sequence / 1000) == 4) //跳过千位4
            {
                newAuthId.Sequence = newAuthId.Sequence + 1000;
            }

            if ((newAuthId.Sequence % 1000 / 100) == 4) //跳过百位4
            {
                newAuthId.Sequence = newAuthId.Sequence + 100;
            }

            if ((newAuthId.Sequence % 100 / 10) == 4) //跳过十位4
            {
                newAuthId.Sequence = newAuthId.Sequence + 10;
            }

            if ((newAuthId.Sequence % 10) == 4) //跳过个4
            {
                newAuthId.Sequence = newAuthId.Sequence + 1;
            }

            string provinceCode;
            if (!ProvinceMap().TryGetValue(province, out provinceCode))
            {
                return null;
            }

            string storeTypeCode;
            if (!StoreTypeMap().TryGetValue(storeType, out storeTypeCode))
            {
                return null;
            }

            newAuthId.Number = "A" + provinceCode + storeTypeCode + newAuthId.Sequence.ToString().PadLeft(4, '0');

            return newAuthId;
        }

        public static Dictionary<string, string> ProvinceMap()
        {
            var provinceMap = new Dictionary<string, string>
            {
                {"上海", "020"}, {"江苏", "100"}, {"安徽", "110"}, {"山东", "120"},
                {"浙江", "130"}, {"江西", "140"}, {"福建", "150"}, {"湖南", "160"},
                {"广东", "190"}, {"海南", "200"}, {"广西", "210"}, {"云南", "240"},
                {"湖北", "170"}, {"河南", "180"}, {"贵州", "220"}, {"四川", "230"},
                {"陕西", "250"}, {"甘肃", "260"}, {"宁夏", "270"}, {"青海", "280"},
                {"新疆", "290"}, {"西藏", "300"}, {"重庆", "320"}, {"北京", "010"},
                {"天津", "030"}, {"内蒙古", "040"}, {"山西", "050"}, {"河北", "060"},
                {"辽宁", "070"}, {"吉林", "080"}, {"黑龙江", "090"}
            };

            return provinceMap;
        }

        public static Dictionary<string, string> StoreTypeMap()
        {
            var storeTypeMap = new Dictionary<string, string>
            {
                {"DDCN", "1"}, {"DCSS", "2"}, {"DIS", "3"},
                {"PR", "4"}, {"MT", "5"}, {"LCSS", "6"},
                {"LIS", "7"}, {"HDC", "8"}, {"E-COMMERCE", "9"}, {"公司仓库", "W"},{"EPS","0"}
            };
            
            return storeTypeMap;
        } 

    }
}